const { DataTypes } = require('sequelize')
const { sequelize } = require('../config/database')

const UserFavorite = sequelize.define('UserFavorite', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  userId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    references: {
      model: 'Users',
      key: 'id'
    }
  },
  mangaId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    references: {
      model: 'Mangas',
      key: 'id'
    }
  },
  isFollowing: {
    type: DataTypes.BOOLEAN,
    defaultValue: true
  },
  lastReadChapter: {
    type: DataTypes.INTEGER,
    allowNull: true
  },
  lastReadPage: {
    type: DataTypes.INTEGER,
    defaultValue: 0
  },
  readProgress: {
    type: DataTypes.JSON,
    defaultValue: {}
  },
  notifyNewChapter: {
    type: DataTypes.BOOLEAN,
    defaultValue: true
  }
}, {
  indexes: [
    {
      unique: true,
      fields: ['userId', 'mangaId']
    }
  ]
})

module.exports = UserFavorite
